package com.tff.gmall.manger.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.tff.gmall.beans.PmsProductImage;
import com.tff.gmall.beans.PmsProductInfo;
import com.tff.gmall.beans.PmsProductSaleAttr;
import com.tff.gmall.beans.PmsProductSaleAttrValue;
import com.tff.gmall.manger.mapper.PmsProductImageMapper;
import com.tff.gmall.manger.mapper.PmsProductSaleAttrMapper;
import com.tff.gmall.manger.mapper.PmsProductSaleAttrValueMapper;
import com.tff.gmall.manger.mapper.SpuMapper;
import com.tff.gmall.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private PmsProductImageMapper productImageMapper;
    @Autowired
    private PmsProductSaleAttrMapper productSaleAttrMapper;
    @Autowired
    private PmsProductSaleAttrValueMapper productSaleAttrValueMapper;

    @Override
    public List<PmsProductInfo> spuListService(String catalog3Id) {
        return spuMapper.spuListMapper(catalog3Id);
    }

    /***
     * 保存商品信息
     * @param pmsProductInfo
     * @return
     */
    @Override
    public boolean saveSpuInfo(PmsProductInfo pmsProductInfo) {
        //保存商品基本信息(会有主键返回策略)
        Integer integer = spuMapper.savePmsProductInfo(pmsProductInfo);

        List<PmsProductSaleAttr> pmsProductSaleAttrs = pmsProductInfo.getSpuSaleAttrList();
        for (PmsProductSaleAttr pmsProductSaleAttr : pmsProductSaleAttrs) {
            //根据主键返回策略设置商品id
            pmsProductSaleAttr.setProductId(pmsProductInfo.getId());
            productSaleAttrMapper.savePmsProductSaleAttr(pmsProductSaleAttr);

            //插入属性值对应的值
            List<PmsProductSaleAttrValue> spuSaleAttrValueList = pmsProductSaleAttr.getSpuSaleAttrValueList();
            for (PmsProductSaleAttrValue value : spuSaleAttrValueList) {
                value.setProductId(pmsProductInfo.getId());
                productSaleAttrValueMapper.savePmsProductSaleAttrValue(value);
            }
        }
        //保存商品的图片
        List<PmsProductImage> pmsProductImages = pmsProductInfo.getSpuImageList();
        for (PmsProductImage pmsProductImage : pmsProductImages) {
            pmsProductImage.setProductId(pmsProductInfo.getId());
            productImageMapper.savePmsProductImage(pmsProductImage);
        }
        return integer > 0 ? true : false;
    }

    /***
     * 返回商品的销售属性及属性值
     * @param spuId
     * @return
     */
    @Override
    public List<PmsProductSaleAttr> spuSaleAttrListService(String spuId) {

        List<PmsProductSaleAttr> pmsProductSaleAttrList = new ArrayList<>();

        List<PmsProductSaleAttr> pmsProductSaleAttrs = productSaleAttrMapper.spuSaleAttrListMapper(spuId);

        for (PmsProductSaleAttr ps : pmsProductSaleAttrs) {
            List<PmsProductSaleAttrValue> productSaleAttrValues = productSaleAttrValueMapper.getPmsProductSaleAttrValueBySaleAttrId(ps);
            ps.setSpuSaleAttrValueList(productSaleAttrValues);

            pmsProductSaleAttrList.add(ps);
        }
        return pmsProductSaleAttrs;
    }

    /***
     * 根据商品id查询商品的所有图片
     * @param spuId
     * @return
     */
    @Override
    public List<PmsProductImage> spuImageListService(String spuId) {
        List<PmsProductImage> pmsProductImages = productImageMapper.spuImageListMapperByProductId(spuId);
        return pmsProductImages;
    }

    /***
     * 根据当前sku的spuid查询销售属性列表
     * @param productId  @param skuid
     * @return
     */
    @Override
    public List<PmsProductSaleAttr> spuSaleAttrListCheckBySku(String productId,String skuId) {

        List<PmsProductSaleAttr> pmsProductSaleAttrs = productSaleAttrMapper.spuSaleAttrListCheckBySku(productId,skuId);
        for (PmsProductSaleAttr ps: pmsProductSaleAttrs) {
            List<PmsProductSaleAttrValue> pmsProductSaleAttrValueBySaleAttrId = productSaleAttrValueMapper.getPmsProductSaleAttrValueBySaleAttrId(ps);
            ps.setSpuSaleAttrValueList(pmsProductSaleAttrValueBySaleAttrId);
        }
        return pmsProductSaleAttrs;
    }
}
